﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Collections;
using pos.entity;


namespace idea.tools
{
    class PrintTool
    {
        
    
        private List<Object> printList;
        private Font m_font1;
        private Font m_font2;
        private Font m_font3;
        public PrintTool()
        {
          

        }
        public void printCommodity(List<Object> list,Font font1,Font font2,Font font3)
        {
            printList = list;
            m_font1 = font1;
            m_font2 = font2;
            m_font3 = font3;
            PrintDocument printPage = new PrintDocument();
            printPage.PrintPage+= Commodity_PrintPage;

            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printPage;

            
            //printDialog.PrinterSettings
            printDialog.PrinterSettings.PrinterName = consts.SettingPara["tagPrinterName"].ToString();
            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {

                  
                    printPage.Print();


                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // printDocument1.PrintController.OnEndPrint(printDocument1,new PrintEventArgs());
                }
            }
        }
         
        public void printCommodityA4(List<Object> list, Font font1, Font font2, Font font3)
        {
            printList = list;
            m_font1 = font1;
            m_font2 = font2;
            m_font3 = font3;
            PrintDocument printPage = new PrintDocument();
            printPage.PrintPage += Commodity_PrintPageA4;

            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printPage;
            


            //printDialog.PrinterSettings
            printDialog.PrinterSettings.PrinterName = consts.SettingPara["tagPrinterName"].ToString();

            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {


                    
                    printPage.Print();


                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // printDocument1.PrintController.OnEndPrint(printDocument1,new PrintEventArgs());
                }
            }
        }
     
        
        private void Commodity_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // e.PageSettings.Landscape = true;
            Graphics g = e.Graphics; //获得绘图对象
            //  float linesPerPage = 0; //页面的行号
            //  float yPosition = 0;   //绘制字符串的纵向位置
            int count = 0; //行计数器
            //  float leftMargin = 0; //左边距
            //    float topMargin = 0; //上边距
            //    string line = null;// 行字符串

            SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
            SolidBrush redBrush = new SolidBrush(Color.Red);//刷子

            /*Pen p = new Pen(myBrush);
          
                             int x,y;
                                   x=0;y=0;
                                   Rectangle rect=new Rectangle(0,0,10,10);
                                   for(int i=0;i<40;i++)
                                   {
                                       for(int j=0;j<300;j++)
                                       {
                                           g.DrawRectangle(p, i*10, j*10, 10, 10);
                                       }
                
                                   }
         
           */
            try
            {


                int pageSize = (int)getX("posCommodityLabel");
                float pageHeight = getY("posCommodityLabel");
                float x1 = getX("posCommodityName");
                float x2 = getX("posCommoditySpec");
                float x3 = getX("posCommodityFactory");
                float x4 = getX("posCommodityMark");
                float x5 = getX("posCommodityGrade");
                float x6 = getX("posCommodityUnit");
                float x7 = getX("posCommodityID");
                float x8 = getX("posRetailPrice");

                float y1 = getY("posCommodityName");
                float y2 = getY("posCommoditySpec");
                float y3 = getY("posCommodityFactory");
                float y4 = getY("posCommodityMark");
                float y5 = getY("posCommodityGrade");
                float y6 = getY("posCommodityUnit");
                float y7 = getY("posCommodityID");
                float y8 = getY("posRetailPrice");
              
                int pageCount = 0;
                foreach ( Object obj in printList)
                {
                     if (pageCount < pageSize)
                        pageCount++;
                    else
                    {
                        e.HasMorePages = true;
                        break;

                    }

                    t_commodity commodity = (t_commodity)obj;

                    int x = count % pageSize;
                    count++;
                    
                    float m = getMoreOrLessY(x+1) + pageHeight * x;
                 

                    
                    g.DrawString(commodity.commodityName, m_font1, myBrush, x1, m+y1);
                    g.DrawString(commodity.commoditySpec, m_font3, myBrush, x2, m + y2);
                    g.DrawString(commodity.commodityFactory, m_font3, myBrush, x3, m+y3);
                    g.DrawString(commodity.mark, m_font3, myBrush, x4, m + y4);
                    g.DrawString(commodity.grade, m_font3, myBrush, x5, m+ y5);
                    g.DrawString(commodity.unit, m_font3, myBrush, x6, m + y6);
                    g.DrawString(commodity.commodityID, m_font3, myBrush, x7, m+ y7);
                    g.DrawString(string.Format("￥{0:F2}", commodity.retailPrice), m_font2, redBrush, x8, m + y8);
                    
                   
                   


                }
                for (int i = 0; i < pageCount; i++)
                {
                    if (printList.Count > 0)
                        printList.RemoveAt(0);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            // g.DrawString(row2, printFont, myBrush, leftMargin, yPosition);

        }
        private void Commodity_PrintPageA4(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            idea.tools.EAN13 en = new EAN13();
            // e.PageSettings.Landscape = true;
            Graphics g = e.Graphics; //获得绘图对象
         

            SolidBrush blackBrush = new SolidBrush(Color.Black);//刷子
            SolidBrush redBrush = new SolidBrush(Color.Red);//刷子
            SolidBrush yellowBrush = new SolidBrush(Color.Yellow);//刷子
            SolidBrush greenBrush = new SolidBrush(Color.Green);//刷子
            SolidBrush whiteBrush = new SolidBrush(Color.White);//刷子
            float l1 = 1;
            float l2 = 2;
            Pen green2 = new Pen(greenBrush,l2);
            Pen green1 = new Pen(greenBrush, l1);
           
            try
            {
                e.PageSettings.Margins.Left = 12;
                e.PageSettings.Margins.Right = 12;
                e.PageSettings.Margins.Top = 0;
                e.PageSettings.Margins.Bottom = 0;


                float t=e.MarginBounds.Width;

         
                float pageHeight = getY("posCommodityLabel");
               
                int pageCount = 0;
                int lx =5, rx = 15, topy = 0, bottomy = 30, w = (e.PageSettings.PaperSize.Width - lx - 2 * rx-e.PageSettings.Margins.Left-e.PageSettings.Margins.Right) / 2, h = (int)pageHeight - topy - bottomy;

              
                PointF[] pp=new PointF[2];

                float[] dashValues = { 5, 5, 5, 5 };

                Pen dashPen = new Pen(Color.Black, 1);

                dashPen.DashPattern = dashValues;

                g.DrawLines(dashPen, pp);
                bool rightFlag = false;
                int count;
                for (int i = 0; i < printList.Count; i++)
                {
                    pageCount = i;
                    count = (int)(i / 2);
                    if (i >= 14)
                    {
                        e.HasMorePages = true;
                        break;

                    }
                 
                    t_commodity commodity = (t_commodity)printList[i];

                 
                    float xx1 = lx;
                    if (rightFlag)
                        xx1 = xx1 + w + 2 * rx;
                    float xx2 = xx1 + w;

                    float yy1 = topy+(count * pageHeight);

                    float u=h / 5;
                    float v = (h - u - 5 - u + 10)/4;
                    float yy2 = yy1 + u+5;
                    float yy3 = yy2 +  v;
                    float yy4 = yy3 +  v;
                    float yy5 = yy1+h - u + 10;
                    float yy6 = yy1+h;
                    
                   
                   
                    StringFormat sf=new StringFormat();
                    sf.Alignment=StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;

                    StringFormat sfLeft = new StringFormat();
                    sfLeft.Alignment = StringAlignment.Center;
                    sfLeft.LineAlignment = StringAlignment.Center;

                    Font font1_b = new Font("宋体", 15,FontStyle.Bold);
                    Font font = new Font("宋体", 8);
                    Font font_v = new Font("宋体", 10);
                    Font fontb = new Font("宋体", 7, FontStyle.Italic );
                    //r1


                    

                    float pv = 30;
                    float w1 = xx1;
                    float w2 = w1+pv;
                    float w3 = w1+pv + (w / 2 - 2 * pv) / 2+pv;
                    float w4 = w3 + pv;
                    float w5 = xx1+w/2+pv/2;
                    float w6 = w5  +pv;
                    float w7 = w6 + pv+pv;
                    float w8 = w7 + pv+pv;
                    float w9 =  xx1+w;


                    g.FillRectangle(yellowBrush, w5, yy3, w9-w5, yy5 - yy3);//yello
                    g.DrawLine(green1, xx1, yy2, xx2, yy2);

                    g.DrawRectangle(green1, w5, yy3, w9-w5, yy5 - yy3);//green yello 


                    g.DrawRectangle(green2, xx1, yy1, w, h);

                    //shu
                    if (!rightFlag)
                        g.DrawLine(dashPen, xx2 + rx, yy1, xx2 + rx, yy6 + bottomy);
                    //hen
                    g.DrawLine(dashPen,
                        xx1,
                       yy6 + bottomy/2,
                       xx2,
                       yy6 + bottomy/2);
                    float pingmingwidth = w / 4 - 30;
                    g.FillRectangle(greenBrush, xx1, yy1, pingmingwidth, yy2 - yy1);
                    g.DrawString("品名", font1_b, whiteBrush, new RectangleF(xx1, yy1, pingmingwidth, yy2 - yy1 + 5), sf);
                    g.DrawString(commodity.commodityName, font1_b, blackBrush, new RectangleF(xx1 + pingmingwidth, yy1, w - pingmingwidth, yy2 - yy1 + 5), sf);
                    string str = string.Format("￥{0:0.0}", commodity.retailPrice);//价格
                   
                    g.DrawString("零售价:", new Font("宋体", 6, FontStyle.Regular), greenBrush, new RectangleF(w5, yy3, w6 - w5 + 10, yy3 - yy2), sfLeft);
                    g.DrawString(str, new Font("宋体", 32, FontStyle.Bold), redBrush, new RectangleF(w5,yy4, w9-w5, yy5 - yy4), sf);

                    g.DrawString("规格", font, greenBrush, new RectangleF(w1, yy2, w2-w1, yy3-yy2), sfLeft);
                    g.DrawString(commodity.commoditySpec, font_v, blackBrush, new RectangleF(w2, yy2, w3 - w2, yy3 - yy2), sf);

                    g.DrawString("产地", font, greenBrush, new RectangleF(w3,yy2, w4-w3,yy3-yy2), sfLeft);
                    g.DrawString(commodity.commodityFactory, font_v, blackBrush, new RectangleF(w4, yy2, w5 - w4, yy3 - yy2), sf);

                    g.DrawString("货号", font, greenBrush, new RectangleF(w5,yy2,w6-w5,yy3-yy2), sfLeft);
                    g.DrawString(commodity.seqID.ToString("0000"), font_v, blackBrush, new RectangleF(w6, yy2, w7 - w6, yy3 - yy2), sf);
                 
                    //货号右边线
                    g.DrawLine(green1, w5, yy2, w5, yy3);
                    g.DrawLine(green1, w5, yy5, w5, yy6);
                     g.DrawString("等级", font, greenBrush, new RectangleF(w1, yy3, w2-w1, yy4-yy3), sfLeft);
                     g.DrawString(commodity.grade, font_v, blackBrush, new RectangleF(w2, yy3, w3 - w2, yy4 - yy3), sf);

                    g.DrawString("单位", font, greenBrush, new RectangleF(w3,yy3, w4-w3,yy4-yy3), sfLeft);
                    g.DrawString(commodity.unit, font_v, blackBrush, new RectangleF(w4, yy3, w5 - w4, yy4 - yy3), sf);

                    g.DrawString("条码", font, greenBrush, new RectangleF(w1,yy4,w2-w1,yy3-yy2), sfLeft);
                    g.DrawString(commodity.commodityID, font_v, blackBrush, new RectangleF(w2, yy4, w4 - w2, yy3 - yy2), sf);

                    if (commodity.commodityID.Length == 13)
                    {
                        Bitmap b = en.GetCodeImage(commodity.commodityID);
                        g.DrawImage(b, new RectangleF(w2 + 20, yy4 + font_v.Height, w4 - w2 - 40, yy3 - yy2 - 5));
                    }
                  //  g.DrawLine(green1, xx1, yy1 + h / 5 * 2, xx1 + w, yy1 + h / 5 * 2);
                  //  g.DrawLine(green1, xx1, yy1 + h / 5 * 3, xx1 + w, yy1 + h / 5 * 3);
                      g.DrawLine(green1, w1,yy5,w9,yy5);
                    g.DrawString("@上海如海",fontb,greenBrush,new RectangleF(w1,yy5,w/2,yy6-yy5),sf);
                    
                 //   float m = getMoreOrLessY(count + 1) + pageHeight * count;

                  


                  //  g.DrawString(commodity.commodityName, m_font1, blackBrush, x1, m + y1);
                  //  g.DrawString(commodity.commoditySpec, m_font3, blackBrush, x2, m + y2);
                 //   g.DrawString(commodity.commodityFactory, m_font3, blackBrush, x3, m + y3);
                  //  g.DrawString(commodity.mark, m_font3, blackBrush, x4, m + y4);

                //    g.DrawString(commodity.grade, m_font3, blackBrush, x5, m + y5);
                 //   g.DrawString(commodity.unit, m_font3, blackBrush, x6, m + y6);
                   // g.DrawString(commodity.commodityID, m_font3, blackBrush, x7, m + y7);
                   // g.DrawString(string.Format("￥{0:F2}", commodity.retailPrice), m_font2, redBrush, x8, m + y8);

                    rightFlag = !rightFlag;
                  


                }
                for (int i = 0; i < pageCount; i++)
                {
                    if (printList.Count > 0)
                        printList.RemoveAt(0);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            // g.DrawString(row2, printFont, myBrush, leftMargin, yPosition);

        }
        private float getX(string str)
        {
            string temp = consts.getPara(str);
            string[] temps = temp.Split(',');
            if (temps.Length != 2)
                throw new Exception("打印参数设置错误!");
            
            return  Convert.ToSingle(temps[0]); ;

        }
        private float getY(string str)
        {
            string temp = consts.getPara(str);
            string[] temps = temp.Split(',');
            if (temps.Length != 2)
                throw new Exception("打印参数设置错误!");

            return Convert.ToSingle(temps[1]); ;

        }
        private float getMoreOrLessY(int y)
        {
            string temp = consts.getPara("posMoreOrLessY");
            string[] temps = temp.Split(',');
            if (temps.Length < y )
                return 0;
            return Convert.ToSingle(temps[y-1]); ;

        }
        
    }
}
